package com.test.sample;

import org.hyperledger.fabric.gateway.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;

public class demo {
    public static void main(String[] args) throws IOException {
        // Load an existing wallet holding identities used to access the network.
        Path walletDirectory = Paths.get("wallet");
        Wallet wallet = Wallets.newFileSystemWallet(walletDirectory);

        // Path to a common connection profile describing the network.
        Path networkConfigFile = Paths.
                get("E:\\github\\fabric-gateway-java\\src\\test\\java\\org\\hyperledger\\fabric\\gateway\\connection-org1.yaml");

        // Configure the gateway connection used to access the network.
        Gateway.Builder builder = Gateway.createBuilder()
                .identity(wallet, "admin")
                .networkConfig(networkConfigFile);

        // Create a gateway connection
        try (Gateway gateway = builder.connect()) {
            // get the network and contract
            Network network = gateway.getNetwork("mychannel");
            Contract contract = network.getContract("fabcar");

            byte[] result;

            result = contract.evaluateTransaction("queryAllCars");
            System.out.println(new String(result));

            contract.submitTransaction("createCar", "CAR11", "BMW", "X5", "BLACK", "Ann");

            result = contract.evaluateTransaction("queryCar", "CAR11");
            System.out.println(new String(result));

            contract.submitTransaction("changeCarOwner", "CAR11", "XZL");

            result = contract.evaluateTransaction("queryCar", "CAR11");
            System.out.println(new String(result));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
